package base.httpclient;

import org.apache.http.HttpEntity;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.protocol.HttpClientContext;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.protocol.HttpContext;

import java.io.IOException;

/**
 * Created by majian8 on 2019/6/14.
 */
public class MyThread2 implements Runnable{
    private final CloseableHttpClient httpClient;
    private final HttpContext context;
    private final HttpGet httpGet;

    public MyThread2(CloseableHttpClient httpClient, HttpGet httpGet) {
        this.httpClient = httpClient;
        //尽管httpClient是线程安全的，但还是强烈建议 没个线程中获取自己的httpContext
        this.context = HttpClientContext.create();
        this.httpGet = httpGet;
    }

    @Override
    public void run() {
        System.out.println(httpGet.getURI().toString());
        try {
            CloseableHttpResponse response = httpClient.execute(
                    httpGet, context);
            try {
                HttpEntity entity = response.getEntity();
            } finally {
                response.close();
            }
        } catch (ClientProtocolException ex) {
            // Handle protocol errors
        } catch (IOException ex) {
            // Handle I/O errors
        }
    }
}
